package com.yc.wxcontroller;

import com.yc.entity.Attendance;
import com.yc.entity.AttendanceRecords;
import com.yc.entity.StudentInfo;
import com.yc.mapper.AttendanceMapper;
import com.yc.service.AttendanceRecordsService;
import com.yc.service.AttendanceService;
import com.yc.service.impl.AttendanceRecordsServiceImpl;
import com.yc.vo.Result;
import com.yc.vo.StudentAttendanceRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhupanlin zxy
 * @version 1.0
 * @description: TODO
 * @date 2023/7/24 11:05
 */
@Api("微信管理考勤详情控制器")
@RestController
@RequestMapping("/wx/records")
public class WXAttendanceRecordsController {
    @Resource
    AttendanceRecordsService attendanceRecordsService;
    @Resource
    private AttendanceRecordsServiceImpl attendanceRecordsServiceimpl;

    @Resource
    AttendanceMapper attendanceMapper;

    @Resource
    AttendanceService attendanceService;

    @ApiOperation("查询学生当天的考勤详情信息")
    @GetMapping("/get/{stuid}")
    public Result selectAttendanceByStuidAndDate(@ApiParam(name = "stuid", value = "当前查询学生的学号", required = true)
                                                 @PathVariable("stuid") String stuid,
                                                 @RequestParam String date){

        Map<String, List<StudentAttendanceRecord>> studentAttendanceRecords = attendanceRecordsService.selectAttendanceByStuidAndDate(stuid, date);
        return Result.success(studentAttendanceRecords);
    }

    @ApiOperation("统计学生当年的考勤信息")
    @GetMapping("/get/all/{stuid}")
    public Result selectAllAttendanceByStudent(@ApiParam(name = "stuid", value = "当前查询学生的学号", required = true)
                                               @PathVariable("stuid") String stuid,
                                               @RequestParam Integer date){
        List<String> dates = attendanceRecordsService.selectNowSchoolYear(date);
        Map<String, Object> records = attendanceRecordsService.selectAllAttendanceByStudent(stuid, dates.get(0), dates.get(1));
        //Map<String, Object> records = attendanceRecordsService.selectAllAttendanceByStudent(stuid, "2023-07-01", "2024-07-01");
        return Result.success(records);
    }

    @ApiOperation("查询学生当月的考勤信息")
    @GetMapping("/get/month/{stuid}")
    public Result selectMonthStatusByStuidAndDate(@ApiParam(name = "stuid", value = "当前查询学生的学号", required = true)
                                                  @PathVariable("stuid") String stuid,
                                                  @RequestParam String date){

        Map<String, List<StudentAttendanceRecord>> studentAttendanceRecords = attendanceRecordsService.selectMonthStatusByStuidAndDate(stuid, date);
        return Result.success(studentAttendanceRecords);
    }

    @ApiOperation("手动添加学生考勤信息")
    @PostMapping("/add/{username}")
    public Result addAttendanceRecords(@ApiParam(name = "username", value = "老师工号", required = true)
            @PathVariable("username") String username,
            @RequestBody List<AttendanceRecords> attendanceRecords){
        if (attendanceRecords.size() > 0){
            attendanceRecordsService.addAttendanceRecords(attendanceRecords, username);
            return Result.success("考勤成功!");
        }
        return Result.error("暂无修改");
    }

    @ApiOperation("学生手动修改学生考勤信息")
    @PostMapping("/modify/{username}")
    public Result modifyAttendanceRecords(@ApiParam(name = "username", value = "学生账号", required = true)
                                              @PathVariable("username") String username,
                                          @RequestBody List<AttendanceRecords> attendanceRecords){
        StudentInfo studentInfo = attendanceMapper.selectStudentInfoByStuid(username);
        //根据学生账号查询班级，然后根据班级查询当前的课程,再查出当前的考勤记录
        final Attendance attendance = attendanceMapper.selectCur(username,studentInfo.getCid());


        attendance.setStuid(username);
        attendanceService.update(attendance);
        if (attendanceRecords.size() > 0){
            attendanceRecordsService.addAttendanceRecords(attendanceRecords, username);
            return Result.success("考勤成功!");
        }
        return Result.error("暂无修改");
    }
    @ApiOperation("老师手动修改学生考勤信息")
    @PostMapping("/teacher/modify/{username}/{classId}")
    public Result teacherModifyAttendanceRecords(@ApiParam(name = "username", value = "老师账号", required = true)
                                          @PathVariable("username") String username,
                                          @RequestBody List<AttendanceRecords> attendanceRecords,@PathVariable("classId") Integer cid ){
        //根据学生账号查询班级，然后根据班级查询当前的课程,再查出当前的考勤记录
        final Attendance attendance = attendanceMapper.selectCurByteacherUsername(username,cid);
        //根据老师工号搜索出的学委信息
        StudentInfo studentInfo=attendanceMapper.selectCommitteeByteacherUsername(username,cid);
        attendance.setStuid(studentInfo.getStuid());
        attendanceService.update(attendance);
        if (attendanceRecords.size() > 0){
            attendanceRecordsService.modifyAttendanceRecords(attendanceRecords, studentInfo.getStuid());
            return Result.success("考勤成功!");
        }
        return Result.error("暂无修改");
    }

    @ApiOperation("获取每个月的总考勤记录")
    @GetMapping("/get/permonth/{stuid}/{status}")
    public Result selectPerMonthAttendance(@ApiParam(name = "stuid", value = "学生学号", required = true)
                                           @PathVariable("stuid") String stuid,
                                           @ApiParam(name = "status", value = "考勤状态", required = true)
                                           @PathVariable("status") String status,
                                           @RequestParam("date") Integer date){
        List<String> dates = attendanceRecordsService.selectNowSchoolYear(date);
        List<Map<String, Object>> res = attendanceRecordsService.selectPerMonthAttendance(stuid, status, dates.get(0), dates.get(1));
        //List<Map<String, Object>> res = attendanceRecordsService.selectPerMonthAttendance(stuid, status,"2023-07-01", "2024-07-01");
        return Result.success(res);
    }

    /**
     * 查询学生的个人评分
     * @author zxy
     * @param stuid
     * @return 最终成绩和图表显示的百分比
     */
    @ApiOperation("查询该学生的个人评分")
    @PostMapping("wx/stuscore")
    public Map<String, Object> stuscore(String stuid) {
        int finalscore = attendanceRecordsServiceimpl.finalscore(stuid);// 最终得分
        // 计算百分比 保留两位四舍五入 最终分数/50
        BigDecimal p = new BigDecimal(Double.valueOf(finalscore) / 50).setScale(2, BigDecimal.ROUND_HALF_UP);
        double percentage = p.doubleValue();// 最终百分比 用于图标进度条显示
        Map<String, Object> SandP = new HashMap<>();
        SandP.put("finalscore", finalscore);
        SandP.put("percentage", percentage);
        return SandP;
    }
}
